﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace SportingGoodsExhibitionSystem
{
    /// <summary>
    /// ApplyBack.xaml 的交互逻辑
    /// </summary>
    public partial class ApplyBack : Page
    {
        public ApplyBack()
        {
            InitializeComponent();
        }
        string strCon = Properties.Resources.strCON;
        int status;
        string stuNum,stuName;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(status==0)
            {
                HealthyTable ht = new HealthyTable(Convert.ToInt32(stuNum));
                ht.Show();
            }
            else if(status==2)
            {
                MessageBox.Show("审核中，无法重复填写");
            }
            else
            {
                MessageBox.Show("已通过，无需重复填写申请");
            }

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (status == 0)
            {
                PromiseBook pb = new PromiseBook();
                pb.Show();
            }
            else if (status == 2)
            {
                MessageBox.Show("审核中，无法重复填写");
            }
            else
            {
                MessageBox.Show("已通过，无需重复填写申请");
            }
            
        }

       

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            if (status == 0)
            {
                BackSche bs = new BackSche();
                bs.Show();
            }
            else if (status == 2)
            {
                MessageBox.Show("审核中，无法重复填写");
            }
            else
            {
                MessageBox.Show("已通过，无需重复填写申请");
            }
            
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (status == 0)
            {
                
                    SqlConnection con = new SqlConnection(strCon);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into applyBack(stunum,name,telephone,agcypeo,agcyrela,agcytele) values(@id,@name,@coll,@rea,@ti,@sta)", con);
                    cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = stuNum;
                    cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = stuName;
                    cmd.Parameters.Add("@coll", SqlDbType.VarChar, 50).Value = teleText.Text;
                    cmd.Parameters.Add("@rea", SqlDbType.VarChar, 50).Value = agrPeo.Text;
                    cmd.Parameters.Add("@ti", SqlDbType.VarChar, 50).Value = agrPeoRela.Text;
                    cmd.Parameters.Add("@sta", SqlDbType.VarChar, 50).Value = teleAgr.Text;
                    cmd.ExecuteNonQuery();
                    cmd = new SqlCommand("update studentInfo set applyStatus=@st where studentNumber=@num", con);
                    cmd.Parameters.Add("@st", SqlDbType.Int, 10).Value = 2;
                    cmd.Parameters.Add("@num", SqlDbType.Int, 10).Value = Convert.ToInt32(stuNum);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("成功提交申请");
                    teleText.Text = "";
                    agrPeo.Text = "";
                    agrPeoRela.Text = "";
                    teleAgr.Text = "";
                
                
            }else if(status == 1)
            {
                MessageBox.Show("已通过，无需重复申请");
            }else if(status == 2)
            {
                MessageBox.Show("审核中，无法重复申请");
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from studentInfo where studentStatus=@num", con);
            cmd.Parameters.Add("@num", SqlDbType.Int, 10).Value = 1;
            SqlDataAdapter read = new SqlDataAdapter(cmd);
            //DataTable dt = new DataTable();
            DataSet dataset = new DataSet();
            read.Fill(dataset);
            DataTable table = dataset.Tables[0];
            DataRowCollection rows = table.Rows;
            DataRow row = rows[0];
            stuNum = row["studentNumber"].ToString();
            labelNum.Content = stuNum;
            stuName = row["studentName"].ToString();
            labelName.Content = row["studentName"].ToString();
            labelCollege.Content = row["studyCollege"].ToString();
            status = Convert.ToInt32(row["applyStatus"].ToString());
            con.Close();
        }
    }
}
